﻿using System;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Modules.ACTP.Components;
using ASPNETVN.PORTAL.Components.Helpers;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEditProject : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditProject_Update;
            this.Delete += AddEditProject_Delete;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Add client event to button.
                btnChooseEmployee.OnClientClick = string.Format("javascript:PopupWindow('{0}', 'window1'); return false;", ResolveUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, "PopupList")));


                // Team.
                ddlTeam.DataSource = Team.SelectCollectionAll(this.ModuleID);
                ddlTeam.DataBind();

                // ProjectType
                ddlProjectType.DataSource = ProjectType.SelectCollectionAll(this.ModuleID);
                ddlProjectType.DataBind();

                if (this.ItemID > 0)
                {
                    Project project = Project.Load(this.ItemID);
                    if (project != null)
                    {
                        ddlTeam.SelectedValue = project.TeamID.ToString();
                        ddlProjectType.SelectedValue = project.ProjectTypeID.ToString();

                        txtCode.Text = project.Code;
                        txtSubject.Text = project.Subject;
                        
                        if (project.StartDate.Year > 1753)
                        {
                            txtStartDate.Text = project.StartDate.ToShortDateString();
                        }

                        if (project.DueDate.Year > 1753)
                        {
                            txtDueDate.Text = project.DueDate.ToShortDateString();
                        }

                        if (project.CompletedDate.Year > 1753)
                        {
                            txtCompletedDate.Text = project.CompletedDate.ToShortDateString();
                        }

                        ddlPriority.SelectedValue = project.Priority;
                        ddlStatus.SelectedValue = project.Status;
                        txtCompletedPercent.Text = project.CompletedPercent.ToString();
                        txtDescription.Text = project.Description;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        void AddEditProject_Update(object sender, EventArgs e)
        {
            Project project = new Project();
            if (this.ItemID > 0)
            {
                project = Project.Load(this.ItemID);
            }

            project.ModuleID = this.ModuleID;
            project.TeamID = Convert.ToInt32(ddlTeam.SelectedValue);
            project.Code = txtCode.Text;
            project.Subject = txtSubject.Text;
            project.ProjectTypeID = Convert.ToInt32(ddlProjectType.SelectedValue);
            project.StartDate = txtStartDate.Text.Length > 0 ? Convert.ToDateTime(txtStartDate.Text) : new DateTime(1753, 1, 1);
            project.DueDate = txtDueDate.Text.Length > 0 ? Convert.ToDateTime(txtDueDate.Text) : new DateTime(1753,1,1);
            project.CompletedDate = txtCompletedDate.Text.Length > 0 ? Convert.ToDateTime(txtCompletedDate.Text) : new DateTime(1753, 1, 1);
            project.Priority = ddlPriority.SelectedValue;
            project.Status = ddlStatus.SelectedValue;
            project.CompletedPercent = Convert.ToByte(txtCompletedPercent.Text);
            project.Description = txtDescription.Text;

            // Insert.
            if (this.ItemID == 0)
            {
                project.CreatedBy = project.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                project.CreatedDate = project.UpdatedDate = DateTime.Now;
                this.ItemID = project.Insert();
            }
            // Update.
            else
            {
                project.ProjectID = this.ItemID;
                project.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                project.UpdatedDate = DateTime.Now;
                project.Update();
            }

            // Go to detail page.
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.ProjectDetail);
            Response.Redirect(url);

        }

        void AddEditProject_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Project.DeleteProject(this.ItemID);
                // Go back.
                this.ReturnToReferrerPage();
            }
        }

        protected void btnUpdateTaskType_Click(object sender, EventArgs e)
        {
            ProjectType.InsertProjectType(this.ModuleID, txtTaskTypeTitle.Text.Trim(), txtDescription.Text);
            ddlProjectType.DataSource = ProjectType.SelectCollectionAll(this.ModuleID);
            ddlProjectType.DataBind();
        }
    }
}
